<template>
	<view class="mine-container" >
		<!--顶部个人信息栏-->
		<view class="content-section">
			<view class="mine-actions grid col-4 text-center">
				<view class="action-item" @click="getCurrentWeek">
					<view class="iconfont icon-jinri text-pink icon"></view>
					<text class="text">本周</text>
				</view>
				<view class="action-item" @click="getBackWeek">
					<view class="iconfont icon-jinyizhoudianjihou text-gray1 icon"></view>
					<text class="text">上一周</text>
				</view>
				<view class="action-item" @click="getCurrentMonth">
					<view class="iconfont icon-benyue  text-yellow icon"></view>
					<text class="text">本月</text>
				</view>
				<view class="action-item" @click="getBackMonth">
					<view class="iconfont icon-yuetongyuebao_0  text-gray2 icon"></view>
					<text class="text">上一月</text>
				</view>
			</view>

			<view>
				<view v-for="(item, index) in tableData" :key="index">
					<uni-card :title="item.date" :extra="item.week" class="card">
						<view class="card-second">
							<view class="card-left">
								<text v-if="item.status == 2">
									缺报
								</text>
								<text v-else-if="item.status == 3">
									无需填报
								</text>
								<text v-else-if="item.status == 4">
									节假日
								</text>
								<text v-else-if="item.status == 5">
									请假
								</text>
								<text v-else-if="item.status == 6">
									调休
								</text>
								<text v-else>{{ item.createTime }}</text>
							</view>
							<view class="card-right">
								<button type="primary" size="mini" v-if="item.status != 1" class="button"
									@click="handleCreate(item.date,index)">
									填报日志
								</button>

								<button type="default" size="mini" v-else-if="item.status == 1" class="button"
									style="margin-left:10px;" @click="handelShow(item.id) ">

									查看详情
								</button>
								<button type="default" size="mini"
									v-if="item.status == 2  " class="button"
									style="margin-left:10px;">
									请假/调休
								</button>
							</view>
						</view>
					</uni-card>
				</view>

			</view>

		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
	import moment from 'moment'
	import {
		getMyProjectAll,
		getMyProjectStatus,
		getMyHourList,
		getMyActorProject,
		createHour,
		leaveHour,
		CancelleaveHour,
		getMyHourListMiss,
		getWorkTypeSelect
	} from "@/api/mhour/project";
	import {
		geApptConfig
	} from "@/api/system/appsSet";
	export default {
		data() {
			return {
				weekArr: [
					"星期日",
					"星期一",
					"星期二",
					"星期三",
					"星期四",
					"星期五",
					"星期六"
				],
				name: this.$store.state.user.name,
				//version: getApp().globalData.config.appInfo.version,
				tableData: [],
				total: 0,
				useHourTotal: 0,
				queryParams: {
					pageNum: 1,
					pageSize: 10
				},
				startDate: "",
				endDate: "",
				setting: "",
				weekFlag: 1,
				monthFlag: 1,

			}
		},
		onLoad() {
			this.getCurrentWeek();
			this.init();
		},
		computed: {
			avatar() {
				return this.$store.state.user.avatar
			},
			windowHeight() {
				return uni.getSystemInfoSync().windowHeight - 50
			}
		},
		methods: {
			handleCreate(hourDate,index) {
				this.$tab.navigateTo('/pages/mhour/create?hourDate=' + hourDate +'&index='+index)
			},
			handelShow(id) {
				//console.log(id);
				this.$tab.navigateTo('/pages/mhour/show?hourId=' + id)
			},

			settings() {
				geApptConfig().then(res => {
					//console.log("settings", res);
					if (res.code == 200) {
						this.setting = res.data;
					}
				});
			},

			reload(index,id){
				
				
				console.log("index",index)
				console.log("id",id)
				
				this.tableData[index].status =1;
				this.tableData[index].id =id;
				this.tableData[index].createTime ='已上报';
				
			},
			
			init() {
				//var start = this.formatDate(this.startDate);
				//var end = this.formatDate(this.endDate)
				getMyHourList(this.endDate, this.startDate).then(
					res => {
						this.total = 0;
						if (res.code == 200) {
							this.tableData = res.data;
							this.tableData.forEach(el => {
								el.showsubmitbtn = new Date(Date.parse(el.date)) < new Date();
								if (el.status == "1" || el.status == "5") {
									el.substatus = false;
								} else {
									el.substatus = true;
								}
								var newdate = new Date(Date.parse(el.date.replace(/-/g, "/")));
								el.week = this.weekArr[newdate.getDay()];
								if (el.createTime != null) {
									el.createTime = el.createTime.substr(0, 19).replace(/T/, " ");
								}
							});
							console.log("tableData", this.tableData)
							if (this.tableData.length <=0) {
								this.$modal.msgError("没有数据，请查询正确的日期。")
							}
							
						}
					}
				);
			},



			handleBuilding() {
				this.$modal.showToast('模块建设中~')
			},

			//获得本周的列表
			getCurrentWeek() {
				this.getCurrentWeekDate();
				this.init()
			},

			//获得上一周的列表
			getBackWeek() {
				this.getBackWeekDate();
				this.init();
			},
			//获得本月的列表
			getCurrentMonth() {
				this.getCurrentMonthDate();
				this.init();
			},
			
			//获取上月的列表
			getBackMonth(){
				
				this.getBackMonthDate();
				this.init();
			},

			//获得本周的开始和结束时间
			getCurrentWeekDate() {
				let start = moment().weekday(1).format('YYYY-MM-DD'); //本周一
				let end = moment().weekday(7).format('YYYY-MM-DD'); //本周日
				this.startDate = start
				this.endDate = end
				this.weekFlag = 1

			},
			//获取某周的开始和结束时间
			getBackWeekDate() {
				let i = this.weekFlag;
				let weekOfDay = parseInt(moment().format('E')); //计算今天是这周第几天
				let start = moment().subtract(weekOfDay + 7 * i - 1, 'days').format('YYYY-MM-DD'); //周一日期
				let end = moment().subtract(weekOfDay + 7 * (i - 1), 'days').format('YYYY-MM-DD'); //周日日期

				this.startDate = start
				this.endDate = end
				this.weekFlag++;
			},

			//获取本月第一天和最后一天
			getCurrentMonthDate(){
				let start = moment(moment().month(moment().month()).startOf('month').valueOf()).format('YYYY-MM-DD');
				let end = moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD');
				
				this.startDate = start
				this.endDate = end
				this.monthFlag = 1;
			},
			
			//获取某月第一天和最后一天
			getBackMonthDate(){
				let i = this.monthFlag;
				let start = moment(moment().month(moment().month() - i).startOf('month').valueOf()).format('YYYY-MM-DD');
				let end  = moment(moment().month(moment().month() - i).endOf('month').valueOf()).format('YYYY-MM-DD');
				
				this.startDate = start
				this.endDate = end
				this.monthFlag++;
			},
			
			


		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}
	
	view {
		font-size: 14px;
		line-height: inherit;
	}

	.mine-container {
		width: 100%;
		height: 100%;


		.header-section {
			padding: 15px 15px 45px 15px;
			background-color: #3c96f3;
			color: white;

			.login-tip {
				font-size: 18px;
				margin-left: 10px;
			}

			.cu-avatar {
				border: 2px solid #eaeaea;

				.icon {
					font-size: 40px;
				}
			}

			.user-info {
				margin-left: 15px;

				.u_title {
					font-size: 18px;
					line-height: 30px;
				}
			}
		}

		.content-section {
			position: relative;
			top: 0px;

			.mine-actions {
				margin: 15px 15px;
				padding: 20px 0px;
				border-radius: 8px;
				background-color: white;

				.action-item {
					.icon {
						font-size: 28px;
					}

					.text {
						display: block;
						font-size: 13px;
						margin: 8px 0px;
					}
				}
			}
		}
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}

	.card {
		margin: 15px 15px;
		box-sizing: border-box;

	}

	.card-second {
		display: flex;
		border-bottom: 1px #EBEEF5 solid;
		flex-direction: row;
		align-items: center;
		padding: 10px;
		overflow: hidden;
	}

	.card-left {
		flex: 1;

	}

	.card-right {
		.button {
			padding-left: 10px;
			padding-right: 10px;

		}
	}
	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
	
	}
	
	
</style>